﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Collections.TreeCollections;
using FeatureModelCore;
using System.Xml.Linq;
using Collections;
using FeatureModel.Collections.TreeArgs;

namespace FeatureModelTool.ProjectView.ProjectTreeStructures
{
    public class ProjectFile : TreeLeaf
    {
        #region XML Sybol
        private readonly static string _XML_FILE_NAME = "FileName";
        #endregion

        #region Properties
        public override string Name { get { return _fileName; } }
        /// <summary>
        /// File Name
        /// </summary>
        public string FileName
        {
            get { return _fileName; }
            set
            {
                this.TreeOwner.PreProcess(this, null); 
                _fileName = value;
                this.TreeOwner.ProProcess(this, null);
            }
        }
        /// <summary>
        /// File Full Path: Folder path + fileName
        /// </summary>
        public string FullFilePath { get { return getFullFilePath(); } }
        #endregion

        #region Fields
        private string _fileName;
        #endregion

        public ProjectFile(TreeNodeIdentify id, string fileName, bool create): base(id)
        {
            _fileName = fileName;
        }

        /// <summary>
        /// args[0]: string filename
        /// </summary>
        /// <param name="id"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static TreeObject ConstructFile(TreeNodeIdentify id, object[] args)
        {
            string filename = string.Empty;
            bool create = false;
            if (args != null)
            {
                filename = args[0].ToString();
                create = (bool)args[1];
            }
            return new ProjectFile(id, filename, create);
        }

        #region XML Parser
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override XElement ToXElem()
        {
            XElement xelem = base.ToXElem();
            xelem.Add(new XAttribute(_XML_FILE_NAME, _fileName));
            return xelem;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xelem"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        internal static void xmlParser(XElement xelem, TreeObject obj)
        {
            ProjectFile file = obj as ProjectFile;
            if (file == null)
            {
                return;
            }
            file._fileName = xelem.Attribute(_XML_FILE_NAME).Value;
        }
        #endregion

        /// <summary>
        /// Get full file path
        /// </summary>
        /// <returns></returns>
        private string getFullFilePath()
        {
            if (this.Parent is ProjectFolder)
            {
                ProjectFolder folder = (ProjectFolder)this.Parent;
                string fullFilePath = folder.FullPath + "\\" + _fileName;
                return fullFilePath;
            }
            else if (this.Parent is ProjectTreeRoot)
            {
                ProjectTreeRoot root = (ProjectTreeRoot)this.Parent;
                string fullFilePath = root.FullPath + "\\" + _fileName;
                return fullFilePath;
            }
            return null;
        }

        public override object Clone()
        {
            throw new NotImplementedException();
        }
    }
}
